<template>
  <div id="app">
    <!-- 头部 -->
    <topbar :key="topbarkey"></topbar>
    <Header></Header>
    <router-view @updatedKey="updatedFn"/>
    <!-- 底部 -->
    <Footer></Footer>
    <login v-show="isShowLoginModal"></login>
    <transition name="slide">
      <toast v-show="isShowToast"></toast>
    </transition>
  </div>
</template>
<script>
import Footer from "./components/Footer.vue";
import Header from "./components/Header.vue";
import Topbar from "./components/Topbar.vue";
import Login from "./components/Login.vue";
import { mapState } from "vuex";
import Toast from "./components/Toast.vue";

export default {
  data() {
    return {
      topbarkey: 0,
    };
  },
  components: {
    Topbar,
    Header,
    Footer,
    Login,
    Toast,
  },
  /* 

    如果项目中请求先请求A再B再C，可能出现回调地狱问题，
    优化方案：使用async + await 使得异步代码同步化

  */
  created() {
    
  },
  computed: {
    ...mapState({
      isShowLoginModal: (state) => state.showModal.isShowLoginModal,
      isShowToast: (state) => state.showToast.isShowToast,
    }),
  },
  methods:{
    updatedFn(){
      this.topbarkey++
    }
  },
  // 组件重载
  watch: {
    "$route.path": {
      handler(newVal, oldVal) {
        // console.log(newVal, oldVal)
        if (oldVal !== "/") { // 因每次刷新页面都会触发地址更新从 / => /新地址，此时会触发两次事件监听，从而执行两次create函数
          // key属性的值一变化，就会做组件重载，从而执行created函数
          console.log("组件重载！！！！");
          this.topbarkey++;
        }
      },
    },
  },
};
</script>
<style lang="less">
/* 入场的起始状态 = 离场的结束状态 */
.slide-enter,
.slide-leave-to {
  opacity: 0;
}

.slide-enter-active,
.slide-leave-active {
  transition: opacity 0.3s linear;
}

.slide-enter-to,
.slide-leave {
  opacity: 1;
}
</style>
